const { Controller } = require('egg');


class ProductController extends Controller {

  /**
   * @api {POST} /order/product/insert 新增产品
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } order_id 订单id
   * @apiBody  { Array } products 产品
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async insert() {
    const ctx = this.ctx;
    const app = this.app;

    try {

      const param = ctx.request.body;

      const { order_id, products } = await app.rule.product(param);

      const data = await ctx.service.product.insertInstance(order_id, products);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {GET} /order/product/product 列表
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } uuid 订单id
   * @apiQuery  { String } order 排序字段
   * @apiQuery  { String } method 排序方式
   * @apiQuery  { String } pageIndex 页码
   * @apiQuery  { String } pageSize 页面大小
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async product() {
    const ctx = this.ctx;
    const app = this.app;

    try {
      const param = ctx.query;

      const { uuid, order, method, pageIndex, pageSize } = await app.rule.product.product(param);

      const data = await ctx.service.product.product(uuid, order, method, pageIndex, pageSize);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}


module.exports = ProductController;
